package data;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import com.EntityName;
import com.Util;
import com.google.appengine.api.datastore.Entity;
import com.hotel.BL.ReservationLogic;

public class SubReservation extends AbstractData {

	private long id = -1;
	private long hotelId;
	private Date date;
	private long span;
	private Date dateTo;
	private Room room;
	private String roomString;
	private RoomType roomType;
	private String roomTypeString;
	private Charge charge;
	private String chargeString;
	private double cost;
	private boolean isAssign;
	private boolean isSellRoom;
	private Date sellRoomDate;
	
	private long parentId;
	
	public SubReservation(){
		this.isSellRoom = false;
	}
	
	public SubReservation(long id)
	{
		this.id = id;
		this.isSellRoom = false;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public long getHotelId() {
		return hotelId;
	}
	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public long getSpan() {
		return span;
	}
	public void setSpan(long span) {
		this.span = span;
		
		this.dateTo = addDays(this.date,span);
	}
	public Date getDateTo() {
		return dateTo;
	}
	public void setDateTo(Date dateTo) {
		this.dateTo = dateTo;

		this.span = TimeUnit.MILLISECONDS.toDays(dateTo.getTime() - this.date.getTime());
	}
	public Room getRoom() {
		if (room == null && !(roomString == null || roomString.isEmpty())) {
			Room r = new Room(Long.parseLong(roomString));
			r.LoadData();
			room = r;
		}
		return room;
	}
	public void setRoom(Room room) {
		this.roomString = String.format("%d", room.getId());
		this.room = room;
	}
	public String getRoomString() {
		return roomString;
	}
	public void setRoomString(String roomString) {
		this.roomString = roomString;
	}
	public RoomType getRoomType() {
		if (roomType == null && !(roomTypeString == null || roomTypeString.isEmpty())) {
			RoomType r = new RoomType(Long.parseLong(roomTypeString));
			r.LoadData();
			roomType = r;
		}
		return roomType;
	}
	public void setRoomType(RoomType roomType) {
		this.roomTypeString = String.format("%d", roomType.getId());
		this.roomType = roomType;
	}
	public String getRoomTypeString() {
		return roomTypeString;
	}
	public void setRoomTypeString(String roomTypeString) {
		this.roomTypeString = roomTypeString;
	}

	public long getParentId() {
		return parentId;
	}
	public void setParentId(long parentId) {
		this.parentId = parentId;
	}
	
	public Reservation getParent()
	{
		Reservation res = new Reservation(parentId);
		if(parentId==-1)
			res.setAssign(this.isAssign);
		res.LoadData();
		return res;
	}

	public Charge getCharge() {
		if (charge == null && !(chargeString == null || chargeString.isEmpty())) {
			Charge r = new Charge(Long.parseLong(chargeString));
			r.LoadData();
			charge = r;
		}
		return charge;
	}

	public void setCharge(Charge charge) {
		this.chargeString = String.format("%d", charge.getId());
		this.charge = charge;
	}

	public String getChargeString() {
		return chargeString;
	}

	public void setChargeString(String chargeString) {
		this.chargeString = chargeString;
	}

	public double getCost() {
		return cost;
	}

	public void setCost(double cost) {
		this.cost = cost;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.SubReservation);
		else
			item = new Entity(this.key);

		item.setProperty("hotelId", this.hotelId);
		item.setProperty("date", this.date);
		item.setProperty("span", this.span);
		item.setProperty("dateTo", this.dateTo);
		item.setProperty("room", this.roomString);
		item.setProperty("roomType", this.roomTypeString);
		item.setProperty("parentId", this.parentId);
		
		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.date = (Date) entity.getProperty("date");
		this.span = (Long) entity.getProperty("span");
		this.dateTo = (Date) entity.getProperty("dateTo");
		this.roomString = (String) entity.getProperty("room");
		this.roomTypeString = (String) entity.getProperty("roomType");
		this.parentId = (Long) entity.getProperty("parentId");

		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.SubReservation, this.id);
			if(entity!=null)setEntity(entity);
		}
	}

	public void CloneReservation(Reservation r)
	{
		setDate(r.getDate());
		this.setDateTo(r.getDateTo());
		this.setHotelId(r.getHotelId());
		this.setParentId(r.getId());
		this.setRoom(r.getRoom());
		this.setRoomString(r.getRoomString());
		this.setRoomType(r.getRoomType());
		this.setRoomTypeString(r.getRoomTypeString());
		this.setSpan(r.getSpan());
		this.setAssign(r.isAssign());
		this.setSellRoom(r.getSellRoom());
		this.setSellRoomDate(Util.addDays(r.getDateTo(),-1));
	}

	public boolean isAssign(ReservationLogic resLogic) {
		if(parentId > 0){
			Reservation res = resLogic.GetReservation(parentId);
			if(res!=null)
				return res.isAssign();
		}
		return isAssign;
	}

	public void setAssign(boolean isAssign) {
		this.isAssign = isAssign;
	}

	public boolean isSellRoom() {
		return isSellRoom;
	}

	public void setSellRoom(boolean isSellRoom) {
		this.isSellRoom = isSellRoom;
	}

	public Date getSellRoomDate() {
		return sellRoomDate;
	}

	public void setSellRoomDate(Date sellRoomDate) {
		this.sellRoomDate = sellRoomDate;
	}
}
